local BigNum = require "BigNum"

-------------------------------------------------------------------------------
---! 内部方法
-------------------------------------------------------------------------------
---! 元表对象
local ITEM_META = {}

function ITEM_META:__index(key)
    local id = self.propId
    if not id then
        return
    end

    local items = IMPORT_D:query("item")
    if not items then
        return
    end

    local item = items[id]
    if not item then
        return
    end

    repeat
        if item.type ~= "weapon" then
            break
        end

        local configs = IMPORT_D:query(item.type)
        if not configs then
            break
        end

        local config = configs[id]
        if not config then
            break
        end

        local val = config[key]
        if not val then
            break
        end

        return val
    until true

    local val = item[key]
    if not val then
        return
    end

    return val
end

function ITEM_META:__newindex(key, val)
    if key ~= "propCount" then
        rawset(self, key, val)
    else
        rawset(self, key, BigNum.new(val))
    end
end

-------------------------------------------------------------------------------
---! 对外接口
-------------------------------------------------------------------------------
ITEM_D = {}

---! 查找道具对象
function ITEM_D:find_item(itemid)
    local configs = IMPORT_D:query("item")
    if not configs then
        return
    end

    local config = configs[itemid]
    if not config then
        return
    end

    local item = { propId = itemid, }
    return setmetatable(item, ITEM_META)
end

---! 查找武器对象
function ITEM_D:find_weapon_by_level(level)
    if level <= 0 or level >= 1000 then
        return
    end

    return ITEM_D:find_item(1000 + level)
end

---! 构建道具对象
function ITEM_D:clone_item(dbase)
    local item = setmetatable({}, ITEM_META)
    return table.update(item, dbase)
end

---! 初始道具数据
function ITEM_D:init_item_info(player)
    ---! 获取初始配置
    local common_config = IMPORT_D:query("common")
    local initialCoins = common_config.initialCoins or 0
    local initialYuanbao = common_config.initialYuanbao or 0
    local initialSoul = common_config.initialSoul or 0
    local initialWeapon = common_config.initialWeapon or 0

    ---! 初始货币
    player:add_currency(GamePropIds.kGamePropIdsCopperIcon, initialCoins)
    ---! 初始水晶
    player:add_currency(GamePropIds.kGamePropIdsDiamondsIcon, initialYuanbao)
    ---! 初始灵魂
    player:add_currency(GamePropIds.kGamePropIdsSoulIcon, initialSoul)
    ---! 初始装备
    player:add_prop(initialWeapon)
end

---! 还原道具数据
function ITEM_D:restore_item_dbase(player, item_dbase)
    if not item_dbase then
        return
    end

    local items = {}
    for key, dbase in pairs(item_dbase) do
        items[key] = ITEM_D:clone_item(dbase)
    end

    player:set_temp("item", items)
end

---! 获取道具数据
function ITEM_D:query_item_dbase(player)
    local items = player:query_temp("item")
    if not items then
        return
    end

    local item_dbase = {}
    for key, item in pairs(items) do
        item = table.copy(item)
        item.propCount = tostring(item.propCount)
        item_dbase[key] = item
    end
    return item_dbase
end

----todo:
--[[
    以下三个消息
    MSGS2CPropInfo
    MSGS2CPropsChange
    MSGS2CItemChange
    使用下述消息进行替换
    MSGS2CItem {
        id := int,      ---! 物品ID, id = 0 时， 表示pos位不存在道具
        pos := int,     ---! 位置
        count := int,   ---! 可选，默认数量为1
        level := int,   ---! 可选，等级
    }
    MSGS2CItemList {
        items := []MSGS2CItem
    }
--]]
function ITEM_D:send_currency_info(userOb)
    local result = {}
    result.Props = INVENTORY_D:get_all_currency(userOb)
    userOb:send_packet("MSGS2CItemChange", result)
end

function ITEM_D:send_props_info(userOb)
    local result = {}
    result.Props = userOb:get_props()
    userOb:send_packet("MSGS2CPropsChange", result)
end

function ITEM_D:send_item_info(userOb)
    local result = {}
    result.props = userOb:get_props()
    result.seniorProps = INVENTORY_D:get_all_currency(userOb)
    userOb:send_packet("MSGS2CPropInfo", result)
end
